/*
 * @Author: AskMeWhy
 * @Date: 2020-12-01 18:05:43
 * @Last Modified by: AskMeWhy
 * @Last Modified time: 2021-02-04 11:05:18
 */

import { Component, Mixins } from "vue-property-decorator";
import FormMixin from "@/mixins/form.mixin.ts";

// const mock = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
@Component
export default class TableMixin extends Mixins(FormMixin) {
    // 表格数据
    currentPage = 1;
    pageSize = 10;
    pageCount = 0;

    tableData: any[] = [];

    // tableData: any[] = mock.map((cur: any, index: number) => {
    //     return {
    //         name: "我是模板名字" + index,
    //         id: index,
    //         departmentValue: [],
    //         status: ""
    //     };
    // });

    getInitParams() {
        return {
            url: "moduleManage/norm-list/list",
            data: {}
        };
    }

    async initData(page = 1) {
        const otherParams: any = this.getInitParams();
        const response = await this.$server["moduleManage/norm-list/list"]({
            data: {
                currentPage: page,
                pageSize: this.pageSize,
                ...otherParams.data
            }
        });
        if (this.responseHasError(response)) {
            return;
        }
        const data = response.data.data || {};
        this.tableData = this.buildTableData(data.records || []);
        this.pageCount = Number(data.total || 0);
        this.currentPage = Number(page);
    }

    buildTableData(list: any) {
        let res: any = [];
        if (list.length === 0) {
            return res;
        }
        res = list.map((cur: any) => {
            return {
                id: cur.id || "",
                name: cur.templateName || "",
                desc: cur.description || "",
                department: cur.departmentNamed || "",
                createByTime: cur.createTime || "",
                createByUser: cur.createUser || "",
                status: cur.status || "",
                source: cur
            };
        });
        return res;
    }

    getConfirmInfo(item: any, type: string, handleModel = "single") {
        const confirmInfo: any = {
            msg: "",
            content: "",
            ids: []
        };
        if (type === "example") {
            confirmInfo.content = "是否确认删除模板?";
            confirmInfo.msg = "删除成功！";
        }
        let ids: any[] = [];
        if (handleModel === "batch") {
            // confirmInfo.content =
            //     "共计" +
            //     this.batchList.length +
            //     "条数据，" +
            //     confirmInfo.content;
            // ids = this.batchList.map(cur => cur.id);
        } else {
            ids = [item.id];
        }
        confirmInfo.ids = ids;
        return confirmInfo;
    }

    triggerConfirm(options: any) {
        const confirmInfo: any = this.getConfirmInfo(
            options.item,
            options.type,
            options.handleModel || "single"
        );
        this.$confirm(confirmInfo.content, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
        })
            .then(() => {
                options.sure && options.sure(confirmInfo, options);
            })
            .catch(() => {
                options.cancel && options.cancel(confirmInfo, options);
            });
    }
}
